package swing;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import java.awt.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class TreeNodeRenderer extends DefaultTreeCellRenderer {
    private static final long serialVersionUID = 8532405600839140757L;

    // 數據庫圖標,頂層節點用
    private static final Icon redisIcon = new ImageIcon("resources/redis.png");

    // 数据库關閉狀態圖標,關閉狀態的第二層節點用
    private static final Icon dbCloseIcon = new ImageIcon("resources/dbclose.png");

    // 数据库關閉狀態圖標,關閉狀態的第二層節點用
    private static final Icon dbOpenIcon = new ImageIcon("resources/dbopen.png");

    // HashKey圖標,第三層節點用
    private static final Icon keyIcon = new ImageIcon("resources/key.png");

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        // 取得節點
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        // 取得路徑
        TreeNode[] paths = node.getPath();
        // 按路径层次赋予不同的图标
        if (paths.length == 3) {
            setIcon(keyIcon);
        }else if(paths.length == 2){
            // 按展開情況再賦予不同的圖標
            if(expanded){
                setIcon(dbOpenIcon);
            } else{
                setIcon(dbCloseIcon);
            }
        } else if(paths.length == 1){
            setIcon(redisIcon);
        }

        this.setOpaque(false);
//        this.setForeground(Color.WHITE);
        this.setTextNonSelectionColor(Color.WHITE);
        this.setTextSelectionColor(new Color(24,144,255));
        this.setBorderSelectionColor(new Color(24,144,255));

        return this;
    }
}